﻿namespace Generic
{
    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    //泛型链表类
    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action)
        {
            for (Node<T> node = head; node != null; node = node.Next)
            {
                action(node.Data);
            }
        }
    }


    internal class Program
    {
        static void Main(string[] args)
        {
            //整型List
            GenericList<int> intlist = new GenericList<int>();
            for (int x = 0; x < 10; ++x)
            {
                intlist.Add(x);
            }
            
            intlist.ForEach(x => Console.WriteLine(x));

            int maxValue = int.MinValue;
            intlist.ForEach(x => maxValue = Math.Max(maxValue, x));
            Console.WriteLine($"Maximum value: {maxValue}");

            int minValue = int.MaxValue;
            intlist.ForEach(x => minValue = Math.Min(minValue, x));
            Console.WriteLine($"Minimum value: {minValue}");

            int sum = 0;
            intlist.ForEach(x => sum += x);
            Console.WriteLine($"Sum: {sum}");
        }
    }
}